//
//  MixingViewController.m
//  CocoaSynth
//
//  Created by Thomas Dupont on 18/02/10.
//  Copyright 2010 Apple Inc. All rights reserved.
//

#import "MixingViewController.h"
#import "FactoryProtocol.h"
#import "MixingView.h"
#import "ModuleViewController.h"
#import "ConnectorLayoutView.h"
#import "ConnectorLayoutViewController.h"
#import "SequencerProtocol.h"
#import "KeyboardViewController.h"
#import "PortViewController.h"

@implementation MixingViewController

@synthesize parameters;
@synthesize sequencer;
@synthesize keyboard;

- (id)init
{
	if (self = [super init]) {
		highlightedModule = nil;
		[self.view setFrame:NSMakeRect(0, 0, 745, 335)];
		
		mixingView = [[MixingView alloc] initWithFrame:NSMakeRect(0, 0, 745, 335)];
		[mixingView setController:self];
		[self.view addSubview:mixingView];
		
		modules = [[NSMutableSet alloc] init];
		
		connectors = [[ConnectorLayoutViewController alloc] init];
		ConnectorLayoutView* connectorView = [[ConnectorLayoutView alloc] initWithFrame:NSMakeRect(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
		[connectors setView:connectorView];
		[self.view addSubview:connectorView];
		
		nbModules = 0;
	}
	return self;
}

- (void)addModule:(ModuleViewController*)aModule
{
	[modules addObject:aModule];
	[aModule setMixingController:self];
	[aModule setName:[NSString stringWithFormat:@"module%i", nbModules]];
	[mixingView addSubview:[aModule view]];
	[aModule displayParameterView];
	[sequencer addModule:[aModule model]];
	nbModules++;
}

- (void)removeModule:(ModuleViewController*)aModule
{
	PortViewController* currentPort;
	
	NSEnumerator* portsInEnum = [[aModule portsIn] objectEnumerator];
	while (currentPort = [portsInEnum nextObject]) {
		[self disconnectPort:currentPort];
	}
	
	NSEnumerator* portsOutEnum = [[aModule portsOut] objectEnumerator];
	while (currentPort = [portsOutEnum nextObject]) {
		[self disconnectPort:currentPort];
	}
	
	[sequencer removeModule:[aModule model]];
	[self removeParameterView];
	highlightedModule = nil;
	[[aModule view] removeFromSuperview];
	[modules removeObject:aModule];
}

#pragma mark -
#pragma mark Ports selection

- (void)selectPortIn:(PortInViewController*)aPort
{
	[connectors selectPortIn:aPort];
}

- (void)selectPortOut:(PortOutViewController*)aPort
{
	[connectors selectPortOut:aPort];
}

- (void)unselectPorts
{
	[connectors unselectPorts];
}

- (void)disconnectPort:(PortViewController*)aPort
{
	[connectors disconnectPort:aPort];
}

- (void)refresh
{
	[connectors refresh];
}

#pragma mark -
#pragma mark Parameters

- (void)removeParameterView
{
	[highlightedModule unhighlight];
	NSArray* subviews = [[parameters view] subviews];
	for (NSView* aSubview in subviews) {
		[aSubview removeFromSuperview];
	}
}

- (void)displayParameterView:(ModuleViewController*)aModule
{
	[self removeParameterView];
	highlightedModule = aModule;
	[aModule highlight];
	[[parameters view] addSubview:[[aModule parameters] view]];
}

- (void)bringSubviewToFront:(NSView*)subview
{
	[mixingView bringSubviewToFront:subview];
}

- (void)dealloc
{
	parameters = nil;
	keyboard = nil;
	[super dealloc];
}
@end
